package cfca.seal.dao;

import cfca.seal.common.Page;
import cfca.seal.system.Env;
import cfca.seal.util.StringUtil;
import java.util.List;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

@Repository
public class PageDao
{
  private static final Logger log = LoggerFactory.getLogger("web_sys");

  @Resource
  private SmartDao smartDao;

  public PageDao() {
  }
  public PageDao(SmartDao smartDao) {
    this.smartDao = smartDao;
  }

  public <T> Page<T> dispage(Class<T> clazz, String tableName, List<String> conditionList, Object[] conditionValue, String pageNoString, int pageSize)
  {
    StringBuilder sql = new StringBuilder();

    StringBuilder sqlCondition = new StringBuilder();
    if ((null != conditionList) && (conditionList.size() > 0)) {
      sqlCondition.append(" WHERE 1 = 1");
      for (int i = 0; i < conditionList.size(); i++) {
        sqlCondition.append(new StringBuilder().append(" AND ").append((String)conditionList.get(i)).toString());
      }

    }

    String countSql = new StringBuilder().append("SELECT COUNT(*) FROM ").append(tableName).append(sqlCondition.toString()).toString();
    int totalCount = this.smartDao.getCount(countSql, conditionValue);
    int pageNo = getPageNo(pageNoString);
    Page page = new Page(totalCount, pageSize, pageNo);

    String databaseType = Env.databaseType;
    if (("mysql".equals(databaseType)) || ("sqlite".equals(databaseType)))
    {
      sql.append(new StringBuilder().append("SELECT * FROM ").append(tableName).toString());
      sql.append(sqlCondition.toString());
      sql.append(" ORDER BY CREATE_TIME DESC LIMIT ");
      sql.append(new StringBuilder().append((pageNo - 1) * pageSize).append(",").append(pageNo * pageSize).toString());
    } else if ("oracle".equals(databaseType))
    {
      sql.append("SELECT * FROM (");
      sql.append(" SELECT A.*, ROWNUM RN FROM (");
      sql.append(new StringBuilder().append(" SELECT * FROM ").append(tableName).toString());
      sql.append(sqlCondition.toString());
      sql.append(" ORDER BY CREATE_TIME DESC");
      sql.append(" ) A WHERE ROWNUM <= ");
      sql.append(pageNo * pageSize);
      sql.append(" ) WHERE RN > ");
      sql.append((pageNo - 1) * pageSize);
    }
    else if ("db2".equals(databaseType))
    {
      sql.append("SELECT * FROM ( ");
      sql.append(" SELECT a.*, rownumber() over() as rowid FROM (");
      sql.append(new StringBuilder().append(" SELECT * FROM ").append(tableName).toString());
      sql.append(sqlCondition.toString());
      sql.append(" ORDER BY CREATE_TIME DESC ");
      sql.append(" ) a ) tmp");
      sql.append(" WHERE tmp.rowid > ");
      sql.append((pageNo - 1) * pageSize);
      sql.append(" AND tmp.rowid <= ");
      sql.append(pageNo * pageSize);
    }

    log.debug(sql.toString());
    List entities = this.smartDao.getList(clazz, sql.toString(), conditionValue);
    page.setBeans(entities);

    return page;
  }

  public static int getPageNo(String pageNoString) {
    int pageNo = 1;
    if (StringUtil.isNotEmpty(pageNoString)) {
      try {
        pageNo = Integer.parseInt(pageNoString);
      } catch (Exception e) {
        pageNo = 1;
      }
    }
    return pageNo;
  }
}